package im.starchat.chat.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.rest.model.ContributionBean;
import im.starchat.chat.view.CircularImageButton;

/**
 * 贡献榜适配器
 */
public class ContributionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private static final int TYPE_NO1 = 1001;
    private static final int TYPE_NO2 = 1002;
    private static final int TYPE_NO3 = 1003;
    private static final int TYPE_NORMAL = 1004;
    private static final int TYPE_EMPTY = 1005;

    private List<ContributionBean> mUserList;

    public ContributionAdapter(Context context) {
        this.mContext = context;
    }


    public void refreshData(List<ContributionBean> list) {
        this.mUserList = list;
        notifyDataSetChanged();
    }

    public void addAll(List<ContributionBean> list){
        this.mUserList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_NO1:
                return new No1ViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_contribution_no1, parent, false));
            case TYPE_NO2:
                return new No2ViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_contribution_no2, parent, false));
            case TYPE_NO3:
                return new No3ViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_contribution_no3, parent, false));
            case TYPE_NORMAL:
                return new NormalViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_contribution_normal, parent, false));
            case TYPE_EMPTY:
            default:
                return new EmptyHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.view_empty, parent, false));

        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_NO1:
                No1ViewHolder no1ViewHolder = (No1ViewHolder) holder;
                if(mUserList.get(position).getUser() != null){
                    if (!TextUtils.isEmpty(mUserList.get(position).getUser().getAvatar())) {
                        Picasso.with(mContext).load(mUserList.get(position).getUser().getAvatar())
                                .placeholder(R.drawable.home_bg)
                                .into(no1ViewHolder.mAvator);
                    }
                    no1ViewHolder.mCoinCount.setText(mUserList.get(position).getAmount() + "");
                    no1ViewHolder.mName.setText(mUserList.get(position).getUser().getNickname());
                    if(mUserList.get(position).getUser().getGender().equals("male")){
                        no1ViewHolder.mSex.setImageResource(R.drawable.icon_male);
                    }else{
                        no1ViewHolder.mSex.setImageResource(R.drawable.icon_female);
                    }
                }

//                emptyHolder.mInfoText.setText("无新消息");
                break;
            case TYPE_NO2:
                No2ViewHolder no2ViewHolder = (No2ViewHolder) holder;
                if(mUserList.get(position).getUser() != null){
                    if (!TextUtils.isEmpty(mUserList.get(position).getUser().getAvatar())) {
                        Picasso.with(mContext).load(mUserList.get(position).getUser().getAvatar())
                                .placeholder(R.drawable.home_bg)
                                .into(no2ViewHolder.mAvator);
                    }
                    no2ViewHolder.mCoinCount.setText(mUserList.get(position).getAmount() + "");
                    no2ViewHolder.mName.setText(mUserList.get(position).getUser().getNickname());
                    if(mUserList.get(position).getUser().getGender().equals("male")){
                        no2ViewHolder.mSex.setImageResource(R.drawable.icon_male);
                    }else{
                        no2ViewHolder.mSex.setImageResource(R.drawable.icon_female);
                    }
                }

//                emptyHolder.mInfoText.setText("无新消息");
                break;
            case TYPE_NO3:
                No3ViewHolder no3ViewHolder = (No3ViewHolder) holder;
                if(mUserList.get(position).getUser() != null){
                    if (!TextUtils.isEmpty(mUserList.get(position).getUser().getAvatar())) {
                        Picasso.with(mContext).load(mUserList.get(position).getUser().getAvatar())
                                .placeholder(R.drawable.home_bg)
                                .into(no3ViewHolder.mAvator);
                    }
                    no3ViewHolder.mCoinCount.setText(mUserList.get(position).getAmount() + "");
                    no3ViewHolder.mName.setText(mUserList.get(position).getUser().getNickname());
                    if(mUserList.get(position).getUser().getGender().equals("male")){
                        no3ViewHolder.mSex.setImageResource(R.drawable.icon_male);
                    }else{
                        no3ViewHolder.mSex.setImageResource(R.drawable.icon_female);
                    }
                }
                break;
            case TYPE_NORMAL:
                NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
                if(mUserList.get(position).getUser() != null){
                    if (!TextUtils.isEmpty(mUserList.get(position).getUser().getAvatar())) {
                        Picasso.with(mContext).load(mUserList.get(position).getUser().getAvatar())
                                .placeholder(R.drawable.home_bg)
                                .into(normalViewHolder.mAvator);
                    }
                    normalViewHolder.mCoinCount.setText(mUserList.get(position).getAmount() + "");
                    normalViewHolder.mName.setText(mUserList.get(position).getUser().getNickname());
                    normalViewHolder.mRankingNumber.setText("No." + (position + 1));
                    if(mUserList.get(position).getUser().getGender().equals("male")){
                        normalViewHolder.mSex.setImageResource(R.drawable.icon_male);
                    }else{
                        normalViewHolder.mSex.setImageResource(R.drawable.icon_female);
                    }
                }
                break;
            case TYPE_EMPTY:
                EmptyHolder emptyHolder = (EmptyHolder) holder;
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(mUserList == null|| mUserList.size() == 0){
            return TYPE_EMPTY;
        }else{
            if(position == 0){
                return TYPE_NO1;
            }else if(position == 1){
                return TYPE_NO2;
            }else if(position == 2) {
                return TYPE_NO3;
            }else{
                return TYPE_NORMAL;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mUserList == null || mUserList.size() == 0) {
            return 1;
        }
        return mUserList.size();
    }

    public class No1ViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.avator)
        CircularImageButton mAvator;
        @InjectView(R.id.name)
        TextView mName;
        @InjectView(R.id.sex)
        ImageView mSex;
        @InjectView(R.id.coin_num)
        TextView mCoinCount;

        public No1ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class No2ViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.avator)
        CircularImageButton mAvator;
        @InjectView(R.id.name)
        TextView mName;
        @InjectView(R.id.sex)
        ImageView mSex;
        @InjectView(R.id.coin_num)
        TextView mCoinCount;

        public No2ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class No3ViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.avator)
        CircularImageButton mAvator;
        @InjectView(R.id.name)
        TextView mName;
        @InjectView(R.id.sex)
        ImageView mSex;
        @InjectView(R.id.coin_num)
        TextView mCoinCount;


        public No3ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class NormalViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.avator)
        CircularImageButton mAvator;
        @InjectView(R.id.name)
        TextView mName;
        @InjectView(R.id.sex)
        ImageView mSex;
        @InjectView(R.id.coin_num)
        TextView mCoinCount;
        @InjectView(R.id.no_number)
        TextView mRankingNumber;



        public NormalViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class EmptyHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.info_text)
        TextView mInfoText;

        public EmptyHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
